%MLPR Assignment 1
%*****************************************************
%Note: Use perplexity as the evaluation metric
%*****************************************************
%Question 2
%*****************************************************
% clear all
% clc
% %Question 2 (a)
% load('imdata.mat');
% %Convert the data to double format
% x = double(x(1:5000,:));
% 
% y = double(y(1:5000,:));

% imshow(imagePatch)

% % %Find the mean image patch
% % meanPatch = mean(x);
% % 
% % %Calculate the covariance of the data
% % covariance = cov(x);
% % 
% % %Calculate the eigenvectors and eigenvalues of the covariance
% % [evecs, evals] = eig(covariance);
% % 
% % %Sort the eigenvalues in descending order
% % [dummy,sortedEvals]=sort(-diag(evals));
% % %Reorder the eigenvalues and eigenvectors into order of descending eigenvalue.
% % evecs=evecs(:,sortedEvals);
% % evals=evals(sortedEvals,sortedEvals);
% % %Extract evals from the diagonal of evals
% % evals=diag(evals);
% % 
% % %Display the mean image patch
% % %Convert the ith row of the x data into an image patch
% % imagePatch = reshape([meanPatch(1,:) zeros(1,18)],35,30);
% % subplot(2,2,1);
% % imagesc(imagePatch);
% % %Display the first three principal vectors
% % p1Patch = reshape([evecs(:,1)' zeros(1,18)],35,30);
% % p2Patch = reshape([evecs(:,2)' zeros(1,18)],35,30);
% % p3Patch = reshape([evecs(:,3)' zeros(1,18)],35,30);
% % 
% % % subplot(2,2,2);
% % % imagesc(p1Patch);
% % % xlabel('The first principal component');
% % % subplot(2,2,3);
% % % imagesc(p2Patch);
% % % xlabel('The first principal component');
% % % subplot(2,2,4);
% % % imagesc(p3Patch);
% % % xlabel('The first principal component');
% % 
% % %Question 2 (b)
% % %*****************************************************
% % %Subtract the mean from each of the data points
% % xNormalised = x - repmat(meanPatch,size(x,1),1);
% % 
% % %Project the datapoints into a 3D space
% % projectedX = [xNormalised*evecs(:,1), xNormalised*evecs(:,2), xNormalised*evecs(:,3)];
% % 
% % %Reconstruct the data
% % reconstructedX = projectedX(:,1)*evecs(:,1)' + projectedX(:,2)*evecs(:,2)' + projectedX(:,3)*evecs(:,3)' + repmat(meanPatch, size(x,1),1);
% % 
% % %Determine the mean squared error of the data
% % %E[xhat - x]^2
% % squaredError  = (x - reconstructedX).^2;
% % MSE = mean(squaredError,2);
% % 
% % %Find the worst item in the training set
% % [value, index] = max(MSE);
% % 
% % %Image patch of this item is:
% % worstImage = reshape([x(index,:) zeros(1,18)],35,30)
% % imagesc(worstImage);

%Important to note: The values in the image are very extreme. This 
%type of variation in the image may be hard to represent.

%Question 2 (c)
%*****************************************************
% % bins  = 0:63;
% % 
% % [Nums, bin] = histc(y, bins);
% % 
% % bar(bins,Nums,'histc');
% % 
% % %More or less unimodal. The values are generally of a low intensity

%Question 2 (d)
%*****************************************************
%Compute the difference between the target variable and the 
%corresponding last attribute in the picture
% targetDifference = y - x(:,end);
% bins = 0:63;
% [Nums, bin] = histc(targetDifference, bins);
% 
% bar( bins, Nums, 'histc');

%Most of the values are near 0. This is expected since
%pixels with similar intensities are being compared as 
%they lie next to one another






